using System;
using System.Collections.Generic;
using FixMath;
using UniFramework.Battle;
using UnityEngine;

namespace UniFramework.Actor
{
	public class LogicBullet : LogicObject
	{
		private BulletObj _bulletObj;
		private List<BulletHitRecord> _hitRecordList = new();
		public BulletObj BulletObj => _bulletObj;
		public List<BulletHitRecord> HitRecordList => _hitRecordList;

		public void InitData(BulletObj bulletObj)
		{
			this._bulletObj = bulletObj;
			LogicPos = bulletObj.firePos;
			LogicAngle = bulletObj.fireAngle;
		}

		public void Move()
		{
			LogicPos += LogicAngle * _bulletObj.speed;
		}

		/// <summary>
		/// 添加击中记录
		/// </summary>
		/// <param name="hitRecord"></param>
		public void AddHitRecord(LogicActor hitRecord)
		{
			_hitRecordList.Add(new BulletHitRecord(hitRecord, _bulletObj.config.sameTargetDelayCheck));
		}

		public bool CanHit(LogicActor target)
		{
			if (_bulletObj.canHitDelay > 0) return false;
			if (_bulletObj.release == target) return false;
			foreach (var hitRecord in _hitRecordList)
			{
				if (hitRecord.target == target) return false;
			}

			//在这里可以添加玩家的状态，比如进行无敌时间的免疫
			return true;
		}

		public override void OnDie()
		{
			base.OnDie();
			_hitRecordList.Clear();
			_hitRecordList = null;
		}
	}
}